package org.ttitfly.ttcore.util;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.dozer.util.mapping.DozerBeanMapper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.ttitfly.ttcore.vo.AbstractVO;


@Component
public class DozerBeanUtil {

    @Autowired
    private DozerBeanMapper mapper;

    private static final Log logger = LogFactory.getLog(DozerBeanUtil.class);

    private static final int timeout = 60;

    private static Timestamp startTime;


    @SuppressWarnings("unchecked")
    public <P> P clone(P base) {
        if (base == null) {
            return null;
        } else {
            return (P) mapper.map(base, base.getClass());
        }

    }

    public <P> List<P> cloneList(List<P> baseList) {

        if (baseList == null) {
            return null;
        } else {
            List<P> targetList = new ArrayList<P>();
            for (P p : baseList) {
                targetList.add((P) clone(p));
            }
            return targetList;
        }

    }

    @SuppressWarnings("unchecked")
    public <V, P> P convertOnOperation(V base, P target) {

        if (base != null) {
            mapper.map(base, target);
            if (AbstractVO.class.isAssignableFrom(target.getClass())) {
//                buildOperations(base, target);
            }
            return target;
        }
        return target;
    }

    @SuppressWarnings("unchecked")
    public <V, P> P convertOnOperation(V base, Class<P> target) {
        if (base == null) {
            return null;
        }
        P p = convert(base, target);
        /**
         * 
         * fill Operations to target VO
         */
        if (AbstractVO.class.isAssignableFrom(target)) {
//            buildOperations(base, p);
        }
        return p;

    }

    public <V, P> List<P> convertListOnOperation(List<V> baseList, Class<P> target) {

        if (baseList == null) {
            return null;
        }
        /*
        if (AbstractVO.class.isAssignableFrom(target) && baseList.size() > 0) {
            return buildOperation4List(baseList, target);
        }
        */
        return convertList(baseList, target);
    }

    @SuppressWarnings("unchecked")
    public <V, P> P convert(V base, Class<P> target) {
        if (base == null) {
            return null;
        } else {
            P p = (P) mapper.map(base, target);
            if (AbstractVO.class.isAssignableFrom(target) && ((AbstractVO) p).getDomainId() != null) {
//                buildDomain(p);
            }

            return p;
        }

    }

    public <V, P> P convert(V base, P target) {
        if (base == null) {
            return null;
        } else {
            mapper.map(base, target);
            if (AbstractVO.class.isAssignableFrom(target.getClass()) && ((AbstractVO) target).getDomainId() != null) {
//                buildDomain(target);
            }

            return target;
        }

    }

    public <V, P> List<P> convertList(List<V> baseList, Class<P> target) {

        if (baseList == null) {
            return null;
        } else {
            List<P> targetList = new ArrayList<P>();
            for (V vo : baseList) {

                targetList.add((P) convert(vo, target));
            }
            return targetList;
        }

    }
    
//    private <P> void buildDomain(P p) {
//        Long key = ((AbstractVO) p).getDomainId();
//        if (DateGen.isTimeout(timeout, startTime) || !domainMap.containsKey(key)) {
//            logger.info("Reload Domain map from Domain Service");
//            domainMap = domainService.getDomainMap();
//            startTime = new Timestamp(System.currentTimeMillis());
//        }
//        if (domainMap.containsKey(key)) {
//            ((AbstractVO) p).setDomainName(domainMap.get(key).getName());
//        }
//    }


}
